// 字符长度必须不小于8位
function checkLen(rule: any, value: any, callback: any) {
  if (value.length >= 8) {
    callback()
  } else {
    callback(new Error('密码长度必须不小于8位'))
  }
}
// 需要包含特殊字符
function checkCharCode(rule: any, value: any, callback: any) {
  let pwdRegex = new RegExp('(?=.*[0-9])(?=.*[A-Za-z])(?=.*[~!@#$%^&*?_-]).{8,30}')
  if (pwdRegex.test(value)) {
    callback()
  } else {
    callback(new Error('密码必须包含字母、数字、特殊字符(可选?,~!@#$%^&*?_-)三种类型'))
  }
}
// 不能连续字符（如123、abc）连续3位或3位以上
function checkRepeat(rule: any, value: any, callback: any) {
  let arr = value.split('')
  let flag = false
  for (let i = 1; i < arr.length - 1; i++) {
    let firstIndex = arr[i - 1].charCodeAt()
    let secondIndex = arr[i].charCodeAt()
    let thirdIndex = arr[i + 1].charCodeAt()
    thirdIndex - secondIndex == 1
    secondIndex - firstIndex == 1
    if (thirdIndex - secondIndex == 1 && secondIndex - firstIndex == 1) {
      flag = true
    }
  }

  if (flag) {
    callback(new Error('密码不能出现顺位数字(字母),如123,abc'))
  } else {
    callback()
  }
}
// 不能相同字符（如111、aaa）连续3位或3位以上
function checkSame(rule: any, value: any, callback: any) {
  let re = /(\w)*(\w)\2{2}(\w)*/g
  if (re.test(value)) {
    callback(new Error('密码不能出现重复数字(字母),如111,aaa'))
  } else {
    callback()
  }
}

const WoValid = {
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    {
      pattern: /^1[23456789]\d{9}$/,
      message: '手机号码格式不正确',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { validator: checkLen, trigger: 'blur' },
    { validator: checkCharCode, trigger: 'blur' },
    { validator: checkRepeat, trigger: 'blur' },
    { validator: checkSame, trigger: 'blur' }
  ],
  smsCode: { required: true, message: '请输入短信验证码', trigger: 'blur' },
  verifyCode: { required: true, message: '请输入验证码', trigger: 'blur' }
}

export default WoValid
